package com.dspsemi.wodi.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dspsemi.wodi.R;
import com.dspsemi.wodi.entity.Player;

public class PlayerListAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<Player> list;
	private Context c;

	public PlayerListAdapter(Context context, ArrayList<Player> it) {
		mInflater = LayoutInflater.from(context);
		c = context;
		list = it;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater
					.inflate(R.layout.item_player_listview, null);
			holder = new ViewHolder();

			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.dengji = (TextView) convertView.findViewById(R.id.dengji);
			holder.jifen = (TextView) convertView.findViewById(R.id.jifen);
			holder.shenglv = (TextView) convertView.findViewById(R.id.shenglv);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.name.setText(list.get(position).getName());
		holder.jifen.setText(list.get(position).getJifen());
		holder.dengji.setText(list.get(position).getDengji());

		float sheng = Float.parseFloat(list.get(position).getSheng());
		float bai = Float.parseFloat(list.get(position).getBai());
		float shenglv = sheng / (sheng + bai);
		// 胜率 = 胜 / （胜+败）;
		holder.shenglv.setText(shenglv * 100 + "%");

		return convertView;
	}

	private class ViewHolder {
		public TextView name;
		public TextView dengji;
		public TextView jifen;
		public TextView shenglv;
	}

}
